package GUI;


import java.io.IOException;
import java.util.ArrayList;

import javax.swing.DefaultCellEditor;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JTable;

import javax.swing.JScrollPane;
import javax.swing.table.TableColumn;

import client.ClientConsole;



import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


/**
 * display the loand of the user and put option for extend loan
 * @author Maor
 *
 */
public class GUI_userLoans extends JPanel {
	
	private ClientConsole clc;
	private JTable table_3;
	/**
	 * the result that back from the server 
	 */
	private ArrayList<String[]> result;
	/**
	 * flag 
	 */
	private int empty;
	/**
	 * panel that we back to him
	 */
	private JPanel ccp;
	
	
	
	/**
	 * constructor that called from librerian menu, when the librerian want to add  new reader card
	 * @param cc  clientConsole
	 * @param gl  the main frame window
	 * @param mmc  student menu, for back
	 * @param readerId of the reader that we want to show his loans
	 */
	public GUI_userLoans(ClientConsole cc,final GUI_LogIn gl,final GUI_MainMenuStudent mmc,final String readerId)
	{
	ccp=mmc;
	shared(cc,gl,readerId);
	}
	
	
	
	/**
	 * constructor that called from manager menu, when the manager want to add  new reader card
	 * @param cc  clientConsole
	 * @param gl  the main frame window
	 * @param erc  manager menu, for back
	 * @param readerId of the reader that we want to show his loans
	 */
	public GUI_userLoans(ClientConsole cc,final GUI_LogIn gl,final Gui_ShowReaderCard erc,final String readerId)
	{
	ccp=erc;
	shared(cc,gl,readerId);	
	}
	
	/**
	 * opening table with loans of the user
	 * user can extend loan
	 */
	private void shared(ClientConsole cc,final GUI_LogIn gl,final String readerId)
	{
		
		this.setSize(600, 600);
		clc=cc;
		String[] s={"loans of user",readerId};
		try {
			clc.client.sendToServer(s);
		    while(clc.client.flag==0)
		    	System.out.print("");
			    clc.client.flag=0;
		  } catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
	
		
		
		result=clc.client.details;
		setLayout(null);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(100, 30, 500, 300);
		add(scrollPane);
		
		
		
		table_3 = new JTable(result.size(),7);
		scrollPane.setViewportView(table_3);
		
		table_3.getColumnModel().getColumn(0).setHeaderValue("loan id");
		table_3.getColumnModel().getColumn(1).setHeaderValue("loan date");
		table_3.getColumnModel().getColumn(2).setHeaderValue("return date");
		table_3.getColumnModel().getColumn(3).setHeaderValue("copy id");
		table_3.getColumnModel().getColumn(4).setHeaderValue("Extend?");
		table_3.getColumnModel().getColumn(5).setHeaderValue("liberian name");
		table_3.getColumnModel().getColumn(6).setHeaderValue("Extend date");
		
		TableColumn col4 = table_3.getColumnModel().getColumn(4);
		JCheckBox cb = new JCheckBox();
		col4.setCellEditor(new DefaultCellEditor(cb));
		
		
		
		JButton btnSubmit = new JButton("submit");
		btnSubmit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				
				empty=1;
				ArrayList<String[]> ar=new ArrayList<String[]>();
			//	String not="the next copies don't extend cause they are " +
				//		"or orderd\n or  there is more then week until the return date\n or :";
				
				for(int i=0;i<result.size();i++)
				{
					if(table_3.getValueAt(i,4).equals(true))
					{
						String who;
						if (clc.rank==0)
							 who="student";
						else who="librerian";
						
						String s[]={"manual extend Books",(String) table_3.getValueAt(i,0),clc.userName,who};
						try {
							clc.client.sendToServer(s);
							table_3.setValueAt(false, i, 4);
							while(clc.client.flag==0)
								System.out.print("");
						    clc.client.flag=0;
						   ar=clc.client.details;
						   if(ar.get(0)[0].equals("orderd"))
						   {
							   JOptionPane.showMessageDialog(new JFrame(),"copy  "+table_3.getValueAt(i,3)+" don't extend casue it already oreded");
						   }
							
						   if(ar.get(0)[0].equals("week"))
						   {
							   JOptionPane.showMessageDialog(new JFrame(),"copy "+table_3.getValueAt(i,3)+" don't extend cause it have more than week until return");
						   }
						   if(ar.get(0)[0].equals("late"))
						   {
							   JOptionPane.showMessageDialog(new JFrame(),"copy "+table_3.getValueAt(i,3)+" don't extend cause you are late in return");
						   }
						} catch (IOException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
				
					}
				}
			
				String[] s={"loans of user",readerId};
				try {
					clc.client.sendToServer(s);
				    while(clc.client.flag==0)
				    	System.out.print("");
					    clc.client.flag=0;
				  } catch (IOException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
			
				result=clc.client.details;
				show_results();
				
			
				}});
		
		
		btnSubmit.setBounds(100, 350, 100, 30);
		add(btnSubmit);
		
		
		
		JButton btnNewButton_1 = new JButton("Back");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.setContentPane(ccp);
			}
		});
		btnNewButton_1.setBounds(300, 350, 100, 30);
		add(btnNewButton_1);
		
		
		JButton btnLogOut = new JButton("Log Out");
		btnLogOut.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				gl.LogOut();
			}
		});
		btnLogOut.setBounds(500, 350, 100, 30);
		add(btnLogOut);
		
		show_results();
		
		
	
		
		
		
	}
	
	/**
	 * put in the table the loan list that back from the server
	 */
	private void show_results() {
	
		
		for(int i=0;i<result.size();i++)
		{
			for(int j=0;j<4;j++)
			{
				table_3.setValueAt(result.get(i)[j],i, j);
			}
			
			for(int j=5;j<7;j++)
			{
				table_3.setValueAt(result.get(i)[j-1],i, j);
			}
			
			table_3.setValueAt(false,i,4);

		}
	}	
		
		

	
	}
